
getClassify();

$(document).ready(function() {
	//初始化文件输入框
	initFileInput("messUrl", urlcore + "/api/attachment/upload", "#messUrlUrl");
	var id = getvl("id");
	if(id != '') {
		$.ajax({
			url: urlcore + "/api/product/selectOne?id=" + id,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					var da = data.data;
					var gmt_datetime = da.gmtDatetime;
					var pic_urlUrl = da.picUrl;
					var imgStr1 = '';
					if(pic_urlUrl != null && pic_urlUrl != '') {
						var imgarr = pic_urlUrl.split("***");
						$.each(imgarr, function(index, value) {
							imgStr1 +=
								'<div style="position:relative;width:250px;height:250px;float:left;" display="block">' +
								'	<span onclick="removeStrHtml(this)" style="position:absolute;top:10px;right:10px;font-size:20px;color:#f00;" class="glyphicon glyphicon-trash" aria-hidden="true">' +
								'	</span>' +
								'	<img style="padding: 5px 5px 5px 5px;" width="250px" height="250px" src="' + urlcore+value + '">' +
								'</div>';
						});
					}
					
					//旧图
					$('#jiu_picture_div').css("display", "block");
					
					$('#classify_name').val(da.classify.name);
					$('#product_name').val(da.productName);
					$('#product_detail').val(da.productDetail);
					$('#init_price').val(da.initPrice);
					$('#final_price').val(da.finalPrice);
					$('#status').val(da.status);
					$('#classify_id').val(da.classifyId);
					$('#gmt_datetime').val(gmt_datetime);
					$('#messUrlUrl').html(imgStr1);
				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;
					if(thisUrl.indexOf('login.html') <= -1) {
						top.window.location.href = "login.html";
					}
				} else {
					alert(data.msg);
				}
			},
			error: function() {
				/* Act on the event */
				alert("error");
			}
		});
		}
	});

  function getJosnData(){  

    alert($("#formID").val());        
	$.ajax({
  	  url:"/support/pages/poapply/PoApply.do?actionFlag=getAllLoginInfoJsonData",
	  type: "GET",
	  dataType: "json",
	  data: { applyFormID:$("#formID").val()},
	  success: function(data){
	  alert("dddd");
	  
		}
	 })
}
   
function getClassify(){
	$.ajax({
        type: "GET",
        url: urlcore +"/api/product/selectByname_id",
        dataType: "json",
        cache: true,
        success: function(res) {
            let [se, str, data] = [document.getElementById('select'), '<option value="">所属产品分类</option>', res.data];
            for (let [i, l]= [0, data.length];i < l;i++) {
            	str += '<option value="'+data[i].id+'">'+data[i].name+'</option>';
            	se.innerHTML = str;
            }
            
        }
    });
}

 
function release() {
	var id = getvl("id");

	var url = "";
	if(id !== "") {
		url = "/api/product/update";
	} else {
		url = "/api/product/add";
	}
	if(id == '') {
		id = null;
	}
	var product_name = $("#product_name").val();
	var product_detail = $("#product_detail").val();
	var init_price = $("#init_price").val();
	var final_price = $("#final_price").val();
	var status = $("#status").val();
	var gmt_datetime = $("#gmt_datetime").val();
	var classify_id = $('#select').val();
	//照片地址
	
	var pic_url = "";
	$.each($("#messUrlUrl img"), function(i,n) {
		pic_url = $(n).attr('src');
	});
	$.ajax({
		url: urlcore + url,
		type: "post",
		data: JSON.stringify({
			"id":id,
			"productName": product_name,
			"picUrl": pic_url,
			"initPrice": init_price,
			"productDetail" :product_detail,
			"finalPrice" : final_price,
			"status" : status,
			"gmtDatetime" : gmt_datetime,
			"classifyId" : classify_id,			
		}),
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				if(id != null) {
					alert('修改成功');
					window.location.href = "product_list.html";
				} else {
					if(confirm("添加成功，是否继续添加？")) {
						//刷新
						location.reload();
					} else {
						//跳转列表页
						window.location.href = "product_list.html";
					}
				}
			} else if(data.code == 'OVERTIME') {
				var thisUrl = window.location.href;
				if(thisUrl.indexOf('login.html') <= -1) {
					top.window.location.href = "login.html";
				}
			} else {
				alert(data.msg);
			}

		},
		error: function() {
			alert("error");
		}

	});
	
}